package com.renwei.yunlong.http;

import android.content.Context;
import android.util.Log;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.renwei.yunlong.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.http
 * Created by :  Benjamin.
 * Created time: 2017/11/23 19:12
 * Changed by :  Benjamin.
 * Changed time: 2017/11/23 19:12
 * Class description:
 */

public class DownloadUtils {
    private final String TAG = "DownloadUtils";
    static final DownloadUtils utils = new DownloadUtils();
    private KProgressHUD mProgressDialog;//进度窗体
    private boolean showDialog = false;

    public synchronized static DownloadUtils getInstance() {
        return utils;
    }

    DownloadLisener downloadLisener;


    public DownloadUtils setDownloadLisener(DownloadLisener downloadLisener) {
        this.downloadLisener = downloadLisener;
        return utils;
    }

    public DownloadUtils showDownLoadDialog(Context context, boolean show) {
        showDialog = show;
        if (context == null) {
            Log.e(TAG, "showDownLoadDialog--- context is null");
            return utils;
        }
        if (mProgressDialog == null) {
            if (showDialog) {
                mProgressDialog = new KProgressHUD(context);
                mProgressDialog.setCancellable(false);
                mProgressDialog.setWindowColor(context.getResources().getColor(R.color.button_gray_color));
            }
        }
        return utils;
    }

    public interface DownloadLisener {
        void onStartedDown();

        void onDownError(Throwable ex, boolean isOnCallback);

        void onDownFinished(File result);
/*

        void onDownLoading(long total, long current);*/
    }


    /**
     * 保证服务端是什么格式的文件在本地就是什么格式的文件
     *
     * @param url
     * @param path 只是本地存放的路径不是本地存放的文件名称
     */
    public void downloadFile(final String url, String path) {
        File file = new File(url);
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String localFileName = path + fileName;
        File localFile = new File(localFileName);
        if (localFile.exists()) {
            if (downloadLisener != null)
                downloadLisener.onDownFinished(localFile);
            return;
        }
        RequestParams requestParams = new RequestParams(url);
        requestParams.setSaveFilePath(localFileName);
        x.http().get(requestParams, new Callback.ProgressCallback<File>() {
            @Override
            public void onWaiting() {
                if (showDialog) {
                    if (mProgressDialog != null) {
                        mProgressDialog.setLabel("加载文件中...");
                        mProgressDialog.show();
                    }
                }
            }

            @Override
            public void onStarted() {
                if (downloadLisener != null)
                    downloadLisener.onStartedDown();
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                if (showDialog) {
                    int max = Long.valueOf(total).intValue();
                    int cur = Long.valueOf(current).intValue();
                    mProgressDialog.setLabel("正在下载\r\n" + max + "/" + cur);
                    mProgressDialog.setMaxProgress(max);
                    mProgressDialog.setProgress(cur);
                    if (max == cur) {
                        mProgressDialog.setLabel("下载完成");
                    }
                }
            }

            @Override
            public void onSuccess(File result) {
                if (showDialog) {
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                }
                if (downloadLisener != null)
                    downloadLisener.onDownFinished(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                Log.e(TAG, "onDownError---" + ex.getMessage());
                if (showDialog) {
                    if (mProgressDialog != null) {
                        mProgressDialog.setLabel("下载失败");
                        mProgressDialog.dismiss();
                    }
                }
                if (downloadLisener != null)
                    downloadLisener.onDownError(ex, isOnCallback);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                hideInfoProgressDialog();
                Log.e(TAG, "onDownFinished--- 下载完成");
            }
        });
    }

    /**
     * 这个地方一定要记得销毁 不然第二次调用的时候会报 addDecorvview到一个已经存在的view上的错误
     */
    public void hideInfoProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }
}
